package com.luckyframe.project.jmeter.execution.sum;

import org.apache.jmeter.control.TransactionController;
import org.apache.jmeter.report.core.Sample;
import org.apache.jmeter.report.core.SampleMetadata;
import org.apache.jmeter.reporters.ResultCollector;
import org.apache.jmeter.reporters.Summariser;
import org.apache.jmeter.samplers.SampleEvent;
import org.apache.jmeter.samplers.SampleResult;
import org.apache.jmeter.samplers.SampleSaveConfiguration;
import org.apache.jmeter.save.CSVSaveService;

import java.util.concurrent.BlockingQueue;

public class CaseResultCollectlor extends ResultCollector {

    private long row=0;

    private BlockingQueue<Sample> samples;

    public CaseResultCollectlor(Summariser summer) {
        super(summer);
    }

    public CaseResultCollectlor(Summariser summer, BlockingQueue<Sample> samples) {
        super(summer);
        this.samples = samples;
    }

    @Override
    public void sampleOccurred(SampleEvent e) {
        super.sampleOccurred(e);

        SampleResult result = e.getResult();

        if (isSampleWanted(result.isSuccessful())){
            SampleResult s = e.getResult();

            if( TransactionController.isFromTransactionController(s)) {
                return;
            }

            SampleMetadata sampleMetadata = new SampleMetadata(
                    SampleSaveConfiguration.staticConfig());

            String delimitedResult = CSVSaveService.resultToDelimitedString(e);
            String[] data = delimitedResult.split(",");

            Sample  sample =new Sample (row++,sampleMetadata,data);
            samples.offer(sample);
        }
    }
}
